package com.zhehekeji.opsys.api.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zhehekeji.opsys.api.cache.OperationCache;
import com.zhehekeji.opsys.api.common.dao.EntityDao;
import com.zhehekeji.opsys.api.enm.SysconfigKey;
import com.zhehekeji.opsys.api.entity.TArea;

/**
 * @ClassName BaseService
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2015-3-26   上午10:09:16
 */
public abstract class CommonService {
	private static final Logger log = LoggerFactory.getLogger(CommonService.class);
	
	@Autowired
	protected EntityDao entityDao;
	@Autowired
	private OperationCache operationCache;
	
	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	public <T> List<T> findPage(Class<T> entityClass, int firstIndex, int maxResult, String whereJpql, Object[] queryParams, String order, String sort) throws SQLException{
    	LinkedHashMap<String, String> orderBy = null;
		if (!StringUtils.isEmpty(order) && !StringUtils.isEmpty(sort)){
			orderBy = new LinkedHashMap<String, String>();
			orderBy.put(sort, order);
		}
		return entityDao.getScrollData(entityClass, firstIndex, maxResult, whereJpql, queryParams, orderBy);
    }
	
	public String getHttpUrl (String httpServer, String picUrl){
		String tmpHttpSeprator = "/";
		String tmpHttpPrefix = "http";
		if (!StringUtils.isEmpty(picUrl)){
			if (!picUrl.startsWith(tmpHttpPrefix) && !StringUtils.isEmpty(httpServer)){
				if (httpServer.endsWith(tmpHttpSeprator)){
					if (picUrl.startsWith(tmpHttpSeprator)){
						picUrl = StringUtils.removeEnd(httpServer, tmpHttpSeprator) + picUrl;
					}else{
						picUrl = httpServer + picUrl;
					}
				}else{
					if (picUrl.startsWith(tmpHttpSeprator)){
						picUrl = httpServer + picUrl;
					}else{
						picUrl = httpServer + tmpHttpSeprator + picUrl;
					}
				}
			}
		}
		return picUrl;
	}
	
	public String getHttpUrl (String picUrl) throws SQLException{
		//log.info("picUrl="+picUrl);
		String tmpHttpServer = operationCache.getSysconfig(SysconfigKey.PIC_SERVER_PATH.getKey());
		//log.info("picServer="+tmpHttpServer);
		return getHttpUrl(tmpHttpServer, picUrl);
	}

	public void operateBySql(String sql) throws SQLException {
		EntityManager entityManager = entityDao.getEntityManager();
		entityManager.flush();
		entityManager.clear();
		entityManager.createNativeQuery(sql).executeUpdate();
	}
	
	public Map<String, String> getAreaByUid(String userID) throws SQLException{
		Map<String, String> map = new HashMap<>();
		
		List<TArea> all = entityDao.getScrollData(TArea.class, -1, -1, "1=1", null, null);
		
        for(TArea area : all){
            String ac = area.getAreaIdentity();
            if(StringUtils.contains(userID, ac)){
                map.put("code", area.getTrueIdentity());
                map.put("name", area.getAreaName());
                break;
            }
        }
		
		return map;
	}
}
